<?php
/**
*@copyright	Joomsoft.net
*@license	Commercial
*@author	Daniel Dinh
*@authorEmail	webdevelopmentvn@gmail.com
*@authorURL	http://joomsoft.net
*/
class plgSystemVmpgalleria extends JPlugin
{


    function onSearch()
    {
    }
function onGetWebServices()
{

}
    /**
    * Do something onAfterInitialise
    */
    function onAfterInitialise()
    {
    }
/**
* Do something onAfterRoute
*/
function onAfterRoute()
{
}
	public function onAfterDispatch()
	{
		// Check that we are in the site application.
		if (JFactory::getApplication()->isAdmin())
		{
			return true;
		}
		$doc = & JFactory::getDocument();
		// Set the variables
		$input = JFactory::getApplication()->input;
		$extension = $input->get('option', '', 'cmd');
		$view = JRequest::getCMD('view');
		if($extension !="virtuemart" && $view!="productdetails")
			return true;

		$doc->addScript(JURI::root()."plugins/system/product_galleria/tmpl/galleria-1.2.6.min.js");
		if (!class_exists( 'VmConfig' )) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart'.DS.'helpers'.DS.'config.php');
		VmConfig::loadConfig();
		// Load the language file of com_virtuemart.
		JFactory::getLanguage()->load('com_virtuemart');
		if (!class_exists( 'calculationHelper' )) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart'.DS.'helpers'.DS.'calculationh.php');
		if (!class_exists( 'CurrencyDisplay' )) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart'.DS.'helpers'.DS.'currencydisplay.php');
		if (!class_exists( 'VirtueMartModelVendor' )) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart'.DS.'models'.DS.'vendor.php');
		if (!class_exists( 'VmImage' )) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart'.DS.'helpers'.DS.'image.php');
		if (!class_exists( 'shopFunctionsF' )) require(JPATH_SITE.DS.'components'.DS.'com_virtuemart'.DS.'helpers'.DS.'shopfunctionsf.php');
		if (!class_exists( 'calculationHelper' )) require(JPATH_COMPONENT_SITE.DS.'helpers'.DS.'cart.php');
		if (!class_exists( 'VirtueMartModelProduct' )){
		   JLoader::import( 'product', JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart' . DS . 'models' );
		}
		$autoplay = $this->params->get('autoplay',1);
		$autoplay_timeout = $this->params->get('autoplay_timeout',400);
		$lightbox = $this->params->get('lightbox') ? 'true' : 'false';
		$gallery_height = $this->params->get('gallery_height',320);
		$gallery_background = $this->params->get('gallery_background');
		$autotext = '';
		if($autoplay)
		{
			$autotext = 'extend: function() {
						this.play('.$autoplay_timeout.'); // will advance every 4th second
					}';
		}
		$virtuemart_product_id = JRequest::getVar('virtuemart_product_id');
		$productModel = VmModel::getModel('Product');
		$product = $productModel->getProduct($virtuemart_product_id);
		$buf = $doc->getBuffer('component');
		$html_images = '';
		if (count($product->images) > 0) :
			foreach ($product->images as $image) :
				$html_images.='<a href="'.  $image->getUrl().'">';
				$html_images.=  $image->displayMediaThumb('class="product-image"', false, 'class="modal"', true, true) ;
				$html_images.=	'</a>';
			endforeach;
		endif;
		$galleria = '<style>
		#galleria{height:'.$gallery_height.'px;}
		.galleria-info{display:none!important;}
		.additional-images{display:none;}
		.galleria-container{background:'.$gallery_background.';}
		</style>';
		$galleria .= '<div id="galleria">'	;
		$galleria .= $html_images;
		$galleria .='</div>';
		$galleria .='<script>
					Galleria.loadTheme("'.JURI::root().'plugins/system/product_galleria/tmpl/galleria.classic.min.js");
					// Initialize Galleria
						jQuery("#galleria").galleria({
						lightbox: '.$lightbox.',
						'.$autotext.'	
					});
					</script>';
		preg_match_all ("/<div class=\"main-image\">([^`]*?)<\/div>/", $buf, $matches);
		if($matches[0])
		{
			$buf = str_replace($matches[0][0],$galleria,$buf);
		}
		$doc->setBuffer($buf, 'component');
		return true;
	}
/**
* Do something onAfterRender
*/
function onAfterRender()
{
}

/**
* Do something onBeforeRender
*/
function onBeforeRender()
{
}
/*

*/
function onBeforeCompileHead()
{

}

}
